<!--
  * 门店表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-26 06:40:44
-->
<template>
  <a-drawer
    :title="form.id ? '门店信息修改' : '新增门店'"
    :width="600"
    :open="visibleFlag"
    @close="onClose"
    :maskClosable="false"
    :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" layout="vertical">
      <a-row>
        <a-col v-if="form.id" :span="12">
          <span>登录账号: {{ form.no }}</span>
        </a-col>
        <a-col v-if="form.id" :span="12">
          <span>门店名称: {{ form.storeName }}</span>
        </a-col>
        <a-col :span="24">
          <span>基础信息</span>
        </a-col>
        <a-col v-if="!form.id" :span="12">
          <a-form-item label="门店名称" name="storeName">
            <a-input style="width: 95%" v-model:value="form.storeName" placeholder="请输入门店名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="门店简称" name="storeAbbrName">
            <a-input style="width: 95%" v-model:value="form.storeAbbrName" placeholder="请输入门店简称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系人" name="name">
            <a-input style="width: 95%" v-model:value="form.name" placeholder="请输入名字" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系方式" name="phone">
            <a-input-number style="width: 95%" v-model:value="form.phone" placeholder="请输入电话" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="城市" name="areaCode">
            <area-cascader style="width: 95%" v-model:value="form.areaCode" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="地址" name="addr">
            <a-input style="width: 95%" v-model:value="form.addr" placeholder="请输入地址" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="门店类型" name="type">
            <SmartEnumSelect width="95%" enum-name="ZZB_STORE_TYPE_ENUM" v-model:value="form.type" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" name="status">
            <SmartEnumSelect width="95%" enum-name="ZZB_ENABLE_STATUS_ENUM" v-model:value="form.status" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="备注" name="remark">
            <a-input style="width: 95%" v-model:value="form.remark" placeholder="请输入备注" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row v-if="!form.id">
        <a-col :span="24">
          <span>账号设置（账号设置为手机号的，将推送验证，登录成功后该账号生效）</span>
        </a-col>
        <a-col :span="12">
          <a-form-item label="账户类型" name="noType">
            <SmartEnumSelect width="95%" enum-name="ZZB_NO_TYPE_ENUM" v-model:value="form.noType" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="登录账号" name="no" help="注意：非手机号账号类型，需大于或小于11位">
            <a-input style="width: 95%" v-model:value="form.no" placeholder="请输入账号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="密码" name="pwd">
            <a-input-password style="width: 95%" :maxlength="20" v-model:value="form.pwd" v-model:visible="visiblePwd" placeholder="请输入密码" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="确认密码" name="conPwd">
            <a-input-password
              style="width: 95%"
              :maxlength="20"
              v-model:value="form.confirmPwd"
              v-model:visible="visibleConfirmPwd"
              placeholder="请输入密码"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
  import { nextTick, reactive, ref } from 'vue'
  import _ from 'lodash'
  import { message } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { storeApi } from '/@/api/business/b-merchant/store/store-api'
  import { smartSentry } from '/@/lib/smart-sentry'
  import AreaCascader from '/@/components/business/b-operation/area-cascader/index.vue'
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { ZZB_NO_TYPE_ENUM } from '/@/constants/system/system-const.js'
  import { isMulPassword } from '/@/utils/validate'

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList'])

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false)
  const visiblePwd = ref(false)
  const visibleConfirmPwd = ref(false)

  function show(rowData) {
    Object.assign(form, formDefault)
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData)
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true
    nextTick(() => {
      formRef.value.clearValidate()
    })
  }

  function onClose() {
    Object.assign(form, formDefault)
    visibleFlag.value = false
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref()

  const formDefault = {
    id: undefined,
    no: undefined, //门店账号
    noType: ZZB_NO_TYPE_ENUM.PHONE_NUMBER.value, //门店账号
    pwd: undefined, //门店密码
    confirmPwd: undefined, //门店密码
    storeName: undefined, //门店名称
    storeAbbrName: undefined, //门店简称
    type: undefined, //门店类型
    areaCode: undefined, //城市，所属区/县ID
    addr: undefined, //地址
    status: undefined, //门店状态，0启用，1禁用
    name: undefined, //名字
    phone: undefined, //电话
    exhibitionId: undefined, //电话
    remark: undefined, //备注
  }

  let form = reactive({ ...formDefault })

  const rules = {
    no: [{ required: true, message: '登录账号 必填' }],
    pwd: [
      { required: true, message: '密码 必填' },
      {
        validator: isMulPassword,
      },
    ],
    confirmPwd: [
      { required: true, message: '密码 必填' },
      {
        validator: isMulPassword,
      },
    ],
    storeName: [{ required: true, message: '门店名称 必填' }],
    storeAbbrName: [{ required: true, message: '门店简称 必填' }],
    type: [{ required: true, message: '门店类型 必填' }],
    areaCode: [{ required: true, message: '城市' }],
    addr: [{ required: true, message: '地址 必填' }],
    status: [{ required: true, message: '状态' }],
    name: [{ required: true, message: '名字 必填' }],
    phone: [{ required: true, message: '电话 必填' }],
  }

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields()
      save()
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!')
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show()
    try {
      if (form.id) {
        await storeApi.update(form)
      } else {
        await storeApi.insert(form)
      }
      message.success('操作成功')
      emits('reloadList')
      onClose()
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }

  defineExpose({
    show,
  })
</script>
